package com.yoouce.patchmng.util;

import com.yoouce.patchmng.bean.PatchInfo;
import com.yoouce.patchmng.bean.PatchInstruction;
import org.apache.log4j.Logger;

import java.io.*;
import java.util.Properties;

/**
 * 读取资源文件工具类
 * Created by Scott.Lee on 15-10-12.
 */
public class PropertiesUtils {
    private static final Logger LOG = Logger.getLogger(PropertiesUtils.class);
    /**
     * 读取资源文件,并处理中文乱码
     */
    public static PatchInfo readPropertiesFile(String filename) {
        Properties properties = new Properties();
        try {
            InputStream inputStream = new FileInputStream(filename);

            properties.load(inputStream);
            inputStream.close(); //关闭流
        } catch (IOException e) {
            e.printStackTrace();
        }
        String operator = properties.getProperty("operator");
        String instruction = properties.getProperty("Instruction");
        String status = properties.getProperty("status");
        String serverIp = properties.getProperty("serverIp");
        String serverUserName = properties.getProperty("serverUserName");
        String serverPassword = properties.getProperty("serverPassword");
        String patchRealName = properties.getProperty("patchRealName");
        String patchName = properties.getProperty("patchName");
        String contextPath = properties.getProperty("contextPath");
        String creatTime = properties.getProperty("creatTime");
        String patchFileName = properties.getProperty("patchFileName");
        try {
            instruction = new String(instruction.getBytes("ISO-8859-1"), "utf-8"); // 处理中文乱码
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        PatchInfo pi = new PatchInfo();
        pi.setInstruction(instruction);
        pi.setOpertor(operator);
        pi.setStatus(Integer.parseInt(status));
        pi.setServerIp(serverIp);
        pi.setServerUserName(serverUserName);
        pi.setServerPassword(serverPassword);
        pi.setPatchRealName(patchRealName);
        pi.setContextPath(contextPath);
        pi.setCreatTime(creatTime);
        pi.setPatchName(patchName);
        pi.setPatchFileName(patchFileName);
        return pi;
    }


    /**
     * 写入资源文件，含中文
     * @param patchInfo 补丁包信息
     * @param fileName 文件路径
     */
    public static void writePropertiesFile(PatchInfo patchInfo, String fileName) {
        Properties properties = new Properties();
        try {
            OutputStream outputStream = new FileOutputStream(fileName);
            properties.setProperty("operator", patchInfo.getOpertor());
            properties.setProperty("Instruction", patchInfo.getInstruction());
            properties.setProperty("status", patchInfo.getStatus().toString());
            properties.setProperty("contextPath", patchInfo.getContextPath());
            properties.setProperty("serverIp", patchInfo.getServerIp());
            properties.setProperty("serverUserName", patchInfo.getServerUserName());
            properties.setProperty("serverPassword", patchInfo.getServerPassword());
            properties.setProperty("patchRealName", patchInfo.getPatchRealName());
            properties.setProperty("creatTime", DateTools.getCurDay(DateTools.yyyyMMddHHmmss));
            properties.setProperty("patchName", patchInfo.getPatchName());
            properties.setProperty("patchFileName", patchInfo.getPatchFileName());
            properties.store(outputStream, "");
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取资源文件,并处理中文乱码
     */
    public static PatchInfo readPropertiesFile(File file) {
        Properties properties = new Properties();
        try {
            InputStream inputStream = new FileInputStream(file);

            properties.load(inputStream);
            inputStream.close(); //关闭流
        } catch (IOException e) {
            e.printStackTrace();
        }
        String operator = properties.getProperty("operator");
        String instruction = properties.getProperty("Instruction");
        String status = properties.getProperty("status");
        String serverIp = properties.getProperty("serverIp");
        String serverUserName = properties.getProperty("serverUserName");
        String serverPassword = properties.getProperty("serverPassword");
        String patchRealName = properties.getProperty("patchRealName");
        String patchName = properties.getProperty("patchName");
        String contextPath = properties.getProperty("contextPath");
        String creatTime = properties.getProperty("creatTime");
        String patchFileName = properties.getProperty("patchFileName");
        try {
            instruction = new String(instruction.getBytes(), "utf-8"); // 处理中文乱码
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        PatchInfo pi = new PatchInfo();
        pi.setInstruction(instruction);
        pi.setOpertor(operator);
        pi.setStatus(Integer.parseInt(status));
        pi.setServerIp(serverIp);
        pi.setServerUserName(serverUserName);
        pi.setServerPassword(serverPassword);
        pi.setPatchRealName(patchRealName);
        pi.setContextPath(contextPath);
        pi.setCreatTime(creatTime);
        pi.setPatchName(patchName);
        pi.setPatchFileName(patchFileName);
        return pi;
    }

    public static void modifyPropertiesValue(String propertiesPath,String propertiesName,String propertiesValue) {
        Properties prop = new Properties();// 属性集合对象
        FileInputStream fis = null;// 属性文件输入流
        try {
            fis = new FileInputStream(propertiesPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            prop.load(fis);// 将属性文件流装载到Properties对象中

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();// 关闭流
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        // 获取属性值，sitename已在文件中定义
        LOG.info("获取属性值：" + propertiesName+"=" + prop.getProperty(propertiesName));
        // 获取属性值，country未在文件中定义，将在此程序中返回一个默认值，但并不修改属性文件
        // System.out.println("获取属性值：country=" + prop.getProperty("country", "中国"));

        // 修改sitename的属性值
        prop.setProperty(propertiesName, propertiesValue);
        // 文件输出流
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(propertiesPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 将Properties集合保存到流中
        try {
            prop.store(fos, "");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fos.close();// 关闭流
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

